package com.sxfq.loansupermarket.backsystem.modular.theTest.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.theTest.dao.TestQuestionsDao;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.SearchTestQuestionsInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TestQuestionsInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.ITestQuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据统计Dao
 *
 * @author fengshuonan
 * @Date 2017-07-19 16:23:35
 */
@Service
public class TestQuestionsInfoServiceImpl implements ITestQuestionsService {

    @Autowired
    private TestQuestionsDao dao;

    @Autowired
    private SqlMapper sqlMapper;


    @Override
    public List<TestQuestionsInfo> selectPageListByQueryGetMap(Page<TestQuestionsInfo> page, SearchTestQuestionsInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getTestTitle())) {
            params.put("testTitle", searchInfo.getTestTitle());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTitleType())) {
            params.put("titleType", searchInfo.getTitleType());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        return dao.getQueryPage(page, params);
    }

    public TestQuestionsInfo selectOneById(String userForTestInfoId) {
        String sql = "select * from test_questions_info where id= '" + userForTestInfoId + "'";
        TestQuestionsInfo testQuestionsInfo = sqlMapper.selectOne(sql, TestQuestionsInfo.class);
        return testQuestionsInfo;
    }

    public int insert(TestQuestionsInfo info) {
        String id = CommUtils.getUUID();
        String sql = "INSERT INTO `test_questions_info` (`id`, `test_title`,`title_type`, `options_a`, `options_b`, `options_c`, `options_d`, `options_e`, `options_f`, `update_people`, `create_people`, `create_time`, `update_time`, `correct_answer`, `remark`) VALUES" +
                "('" + id + "','" + info.getTestTitle().trim() + "','" + info.getTitleType().trim() + "','" + info.getOptionsA().trim() + "','" + info.getOptionsB().trim() + "','" + info.getOptionsC().trim() + "','" + info.getOptionsD().trim() + "'," +
                "'" + info.getOptionsE().trim() + "','" + info.getOptionsF().trim() + "','" + ShiroKit.getUser().getName() + "','" + ShiroKit.getUser().getName() + "','" + DateTimeKit.now() + "','" + DateTimeKit.now() + "','" + info.getCorrectAnswer().trim() + "'," +
                "'" + info.getRemark() + "');";
        return sqlMapper.insert(sql);
    }

    public int update(TestQuestionsInfo info) {
        return dao.updateById(info);
    }

    public void delete(String id) {
        String sql = "delete from test_questions_info  where id = '" + id + "'";
        sqlMapper.delete(sql);
    }

    /*
 * 试题列表
 */
    @Override
    public List<TestQuestionsInfo> selectTestQuestionsInfo() {
        String sql = "select * from test_questions_info";
        List<TestQuestionsInfo> list = sqlMapper.selectList(sql, TestQuestionsInfo.class);
        return list;
    }
}
